Homemade NAS.md
Python script, Unicode text, UTF-8 text executable
title: Cheap, homemade NAS with Raspberry Pi topics: ["hardware", "server", "networking", "raspberry pi", "nas", "gnu/linux"] ---
This is a very simple, cheap and quick way to get networked storage at home. It should not cost more than €120 for all the components (assuming you've got a network you can plug it into). It also offers more flexibility than a commercial NAS, because you can install any software you want on it. And if you already use the Raspberry Pi for something else, you can just add this to it and not worry about an extra device you need power, networking, space and maintenance for.
A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives, since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each.
However, for large-scale use, this method should be done with a different type of computer, not a Raspberry Pi.
I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that; if you don't know these things read some material when you need it.
If you think this is for you, keep reading.
Materials
Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using the Pi 4 with 8GB of RAM, but you can use any model.
microSD card of at least 16GB (this won't be your primary storage but a boot device)
SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer; it's easier that way. Choose ext4 as the filesystem.
Suitable adapter(s) for the SATA drive(s).
For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient.
If your other kind of small computer has SATA, you can use that.
Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one that can take power from a separate source if you're going to use 3.5" drives.
A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed as fast charging) is enough.
If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one.
5" drives need more power than USB can provide.
If you want to use wired networking, a cable. If you want to use wireless networking and your computer doesn't have Wi-Fi, some Wi-Fi adapter.
If you have an SBC you should have a case, but it's not required. You can also use lego.
Some other computer. An Android phone is fine; just install Termux.
My setup
Raspberry Pi 4 with 8GB of RAM (€80)
No-name 3.5" SATA to USB adapter with power supply (€18)
One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC.
Raspberry Pi 15W power adapter (€9)
A 1.5m Cat6 cable (€3)
SanDisk Extreme 128GB microSD card (€15)
Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget Raspberry Pi is not Apple.
Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can certainly have a cheaper setup:
Low-cost setup
Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't have wired networking, only one USB port, and too little RAM.
Cheaper no-name 3.5" SATA to USB adapter with power supply (€10)
One Toshiba P300 1TB 3.5" SATA drive (€50)
Phone charger (€6, if not already owned)
No cable, use Wi-Fi (I assume you have Wi-Fi)
SanDisk Ultra 32GB microSD card (€7)
No case
Total: €113, with drive included!
What about a "real" NAS?
Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else, it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive. (The brand is Synology.)
No, it doesn't come with drives. Using the same drive as above, it would reach €230.
Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty and professional support. However, it's not the kind of NAS a business would use either.
Setup (skip if you already have the computer set up)
Flash your microSD card. Raspberry Pi recommends their own (free) software, Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu enable SSH and preset the Wi-Fi settings if you're going
Put the card in the computer, plug in the network and start it.
Find out the IP address of the computer. You can use your router's web interface, or a network Now is a good time to set up a static IP address for the computer, forward its ports dynamic DNS if you want to access it from the internet. YDNS is a imple and free service for that. Their official client is also free software.
Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled
ssh
On Windows, you can use PuTTY.Set a root password with
sudo passwd
. Thensu
.Update the system with
apt update && apt -y upgrade
.
Set up the drives
You must edit the /etc/fstab
to automatically mount the drives on boot. If you know how to do
this, you can do it yourself. For the rest of the tutorial the mount point will be /storage
.
Turn off the computer and plug in the drive(s).
Turn on the computer.
lsblk
to find the drive(s). They should be/dev/sd
followed by a letter. For now we'llFind the drive's UUID with
blkid /dev/sdX1
.Add the drive to the
/etc/fstab
as such:=00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2
Replace the UUID with the one you found and, if you want, the mount point (/storage
in this
case) with another one, and the filesystem to match the one you formatted the drive with. Change
the last column to 0
to disable fsck for that drive.
mkdir /storage
to create the mount point.Reboot.
Give each user their private directory
We'll mirror the home layout. However, we won't move the existing home directories, because then users can't use the SD card, and you may have some other apps to run on the server that you want to be separate from the mass storage.
Write a systemd service to create the directories on boot. Give it a name inside /etc/systemd/system/
,
like /etc/systemd/system/setup_storage_dirs.service
[Unit] Description=Update private user storage spaces [Service] Type=oneshot ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py [Install] WantedBy=multi-user.target
And the script /usr/local/bin/setup_storage_dirs.py
:
#!/bin/python3 import os import subprocess from pathlib import Path homes = Path("/home").glob("*") storage_path = Path("/storage") # change this if you changed the mount point for home in homes: if home.is_dir(): user = home.name storage = storage_path/user bound = home/"Storage" # change this if you'd like a different name if not storage.exists(): os.makedirs(storage) os.chown(storage, home.stat().st_uid, home.stat().st_gid) os.chmod(storage, 0o700) if not bound.is_dir(): os.makedirs(bound) os.chown(bound, home.stat().st_uid, home.stat().st_gid) os.chmod(bound, 0o700) # Using bind mounts; some file managers don't like symlinks subprocess.run(["mount", "--bind", str(storage), str(bound)])
Reboot. Now each user has a private directory on your drive at ~/Storage
. You can add more
users; each will get their own directory only they can access.
Using the server
This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH) because it's plug-and-play, the speed difference is negligible, it integrates with system users, it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted.
Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem, it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP add-on. Not like Next"cloud"¹ is more integrated.
If you want to mount it there is the sshfs
program as well. This is useful if you don't like
GUI file managers or want easier access to the files.
SFTP in the terminal
sftp -oPort=22 user@server
If you use port 22, you can omit the oPort option. The commands are identical to the ones in the
classic ftp
client.
SSHFS
sshfs user@server:/storage/user ~/server
Second argument is the "mount point". If you want to unmount it:
fusermount -u ~/server
SFTP in the file manager
Graphically, you can access SFTP by typing sftp://user@server
in the address bar (works in most
cases) or by finding an option to connect to a server.
Nemo (GNU/Linux)
File > Connect to server > select type SSH
Material Files (Android)
Menu > Add storage > SFTP server
Amaze (Android)
Plus button > Cloud connection > SCP/SFTP Connection
Windows
Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free software.
Samsung phone file manager (Android)
Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server
1--- 2title: Cheap, homemade NAS with Raspberry Pi 3topics: ["hardware", "server", "networking", "raspberry pi", "nas", "gnu/linux"] 4--- 5 6This is a very simple, cheap and quick way to get networked storage at home. It should not cost 7more than €120 for all the components (assuming you've got a network you can plug it into). It also 8offers more flexibility than a commercial NAS, because you can install any software you want on it. 9And if you already use the Raspberry Pi for something else, you can just add this to it and not 10worry about an extra device you need power, networking, space and maintenance for. 11 12A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives, 13since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each. 14 15However, for large-scale use, this method should be done with a different type of computer, not 16a Raspberry Pi. 17 18I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that; 19if you don't know these things read some material when you need it. 20 21If you think this is for you, keep reading. 22 23Materials 24--------- 25 26* Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using 27the Pi 4 with 8GB of RAM, but you can use any model. 28* microSD card of at least 16GB (this won't be your primary storage but a boot device) 29* SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer; 30it's easier that way. Choose ext4 as the filesystem. 31* Suitable adapter(s) for the SATA drive(s). 32* For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient. 33* If your other kind of small computer has SATA, you can use that. 34* Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a 35SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one 36that can take power from a separate source if you're going to use 3.5" drives. 37* A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed 38as fast charging) is enough. 39* If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one. 403.5" drives need more power than USB can provide. 41* If you want to use wired networking, a cable. If you want to use wireless networking and your 42computer doesn't have Wi-Fi, some Wi-Fi adapter. 43* If you have an SBC you should have a case, but it's not required. You can also use lego. 44* Some other computer. An Android phone is fine; just install [Termux](https://termux.com/). 45 46### My setup 47 48* Raspberry Pi 4 with 8GB of RAM (€80) 49* No-name 3.5" SATA to USB adapter with power supply (€18) 50* One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC. 51* Raspberry Pi 15W power adapter (€9) 52* A 1.5m Cat6 cable (€3) 53* SanDisk Extreme 128GB microSD card (€15) 54* Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget 55Raspberry Pi is not Apple. 56 57Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can 58certainly have a cheaper setup: 59 60### Low-cost setup 61 62* Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't 63have wired networking, only one USB port, and too little RAM. 64* Cheaper no-name 3.5" SATA to USB adapter with power supply (€10) 65* One Toshiba P300 1TB 3.5" SATA drive (€50) 66* Phone charger (€6, if not already owned) 67* No cable, use Wi-Fi (I assume you have Wi-Fi) 68* SanDisk Ultra 32GB microSD card (€7) 69* No case 70 71Total: €113, with drive included! 72 73What about a "real" NAS? 74 75Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else, 76it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive. 77(The brand is Synology.) 78 79No, it doesn't come with drives. Using the same drive as above, it would reach €230. 80 81Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty 82and professional support. However, it's not the kind of NAS a business would use either. 83 84Setup (skip if you already have the computer set up) 85---------------------------------------------------- 86 871. Flash your microSD card. Raspberry Pi recommends [their own (free) software](https://www.raspberrypi.org/software/), 88but there are other methods as well. Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu 89Server or plain Debian. Make sure to enable SSH and preset the Wi-Fi settings if you're going 90to use Wi-Fi. 912. Put the card in the computer, plug in the network and start it. 923. Find out the IP address of the computer. You can use your router's web interface, or a network 93scanner. Now is a good time to set up a static IP address for the computer, forward its ports 94and get dynamic DNS if you want to access it from the internet. [YDNS](https://ydns.io/) is a 95really simple and free service for that. Their official client is also free software. 964. Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled `ssh` 97client. On Windows, you can use [PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html). 985. Set a root password with `sudo passwd`. Then `su`. 996. Update the system with `apt update && apt -y upgrade`. 100 101Set up the drives 102----------------- 103 104You must edit the `/etc/fstab` to automatically mount the drives on boot. If you know how to do 105this, you can do it yourself. For the rest of the tutorial the mount point will be `/storage`. 106 1071. Turn off the computer and plug in the drive(s). 1082. Turn on the computer. 1093. `lsblk` to find the drive(s). They should be `/dev/sd` followed by a letter. For now we'll 110only take car of one drive, but you can do the same for multiple drives. 1114. Find the drive's UUID with `blkid /dev/sdX1`. 1125. Add the drive to the `/etc/fstab` as such: 113``` 114UUID=00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2 115``` 116 117Replace the UUID with the one you found and, if you want, the mount point (`/storage` in this 118case) with another one, and the filesystem to match the one you formatted the drive with. Change 119the last column to `0` to disable fsck for that drive. 1206. `mkdir /storage` to create the mount point. 1217. Reboot. 122 123Give each user their private directory 124-------------------------------------- 125 126We'll mirror the home layout. However, we won't move the existing home directories, because then 127users can't use the SD card, and you may have some other apps to run on the server that you want 128to be separate from the mass storage. 129 130Write a systemd service to create the directories on boot. Give it a name inside `/etc/systemd/system/`, 131like `/etc/systemd/system/setup_storage_dirs.service` 132 133```ini 134[Unit] 135Description=Update private user storage spaces 136 137[Service] 138Type=oneshot 139ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py 140 141[Install] 142WantedBy=multi-user.target 143``` 144 145And the script `/usr/local/bin/setup_storage_dirs.py`: 146 147```python 148#!/bin/python3 149 150import os 151import subprocess 152from pathlib import Path 153 154homes = Path("/home").glob("*") 155storage_path = Path("/storage") # change this if you changed the mount point 156 157for home in homes: 158if home.is_dir(): 159user = home.name 160storage = storage_path/user 161bound = home/"Storage" # change this if you'd like a different name 162if not storage.exists(): 163os.makedirs(storage) 164os.chown(storage, home.stat().st_uid, home.stat().st_gid) 165os.chmod(storage, 0o700) 166if not bound.is_dir(): 167os.makedirs(bound) 168os.chown(bound, home.stat().st_uid, home.stat().st_gid) 169os.chmod(bound, 0o700) 170 171# Using bind mounts; some file managers don't like symlinks 172subprocess.run(["mount", "--bind", str(storage), str(bound)]) 173``` 174 175Reboot. Now each user has a private directory on your drive at `~/Storage`. You can add more 176users; each will get their own directory only they can access. 177 178Using the server 179---------------- 180 181This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH) 182because it's plug-and-play, the speed difference is negligible, it integrates with system users, 183it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted. 184 185Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP 186servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem, 187it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP 188add-on. Not like Next"cloud"¹ is more integrated. 189 190If you want to mount it there is the `sshfs` program as well. This is useful if you don't like 191GUI file managers or want easier access to the files. 192 193### SFTP in the terminal 194 195```sh 196sftp -oPort=22 user@server 197``` 198 199If you use port 22, you can omit the oPort option. The commands are identical to the ones in the 200classic `ftp` client. 201 202### SSHFS 203 204```sh 205sshfs user@server:/storage/user ~/server 206``` 207 208Second argument is the "mount point". If you want to unmount it: 209 210```sh 211fusermount -u ~/server 212``` 213 214### SFTP in the file manager 215 216Graphically, you can access SFTP by typing `sftp://user@server` in the address bar (works in most 217cases) or by finding an option to connect to a server. 218 219#### Nemo (GNU/Linux) 220File > Connect to server > select type `SSH` 221 222#### Material Files (Android) 223Menu > Add storage > SFTP server 224 225#### Amaze (Android) 226Plus button > Cloud connection > SCP/SFTP Connection 227 228#### Windows 229Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free 230software. 231 232#### Samsung phone file manager (Android) 233Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server 234